import os
import shutil

import oss2
import threading

from settings import materials_path


class OSS():
    def __init__(self, AccessKeyID, AccessKeySecret, EndPoint, BucketName):
        self.auth = oss2.Auth(AccessKeyID, AccessKeySecret)
        self.bucket = oss2.Bucket(self.auth, EndPoint, BucketName)

    def get_file(self, object_name="", local_path=""):
        # self.bucket.get_object_to_file("接口.pdf", "C:\\Users\\Administrator\\Desktop\\Project\\autoflow\\original_files\\接口.pdf")
        self.bucket.get_object_to_file(object_name, local_path)

    def get_file_by_stream(self, object_name="", local_path=""):
        object_stream = self.bucket.get_object(object_name)
        with open(local_path, "wb") as local_fileobj:
            shutil.copyfileobj(object_stream, local_fileobj)
        return local_path

    def get_file_by_resumable(self, object_name="", local_path=""):
        if not local_path.endswith(".pdf"):
            local_path = local_path + "\\{}".format(object_name)
        store = "{}\\{}".format(materials_path, "cache")
        if not os.path.exists(store):
            os.mkdir(store)
        class thread_download(threading.Thread):
            this = self
            def run(self):
                oss2.resumable_download(self.this.bucket, object_name, local_path,
                                        store=oss2.ResumableDownloadStore(root=store),
                                        num_threads=threading.active_count())
        t = thread_download()
        t.start()
        t.join()
        return local_path

    def put_file_by_resumable(self, object_name="", local_path=""):
        store = "{}\\{}".format(materials_path, "cache")
        if not os.path.exists(store):
            os.mkdir(store)
        class thread_upload(threading.Thread):
            this = self
            def run(self):
                oss2.resumable_upload(self.this.bucket, object_name, local_path,
                                      store=oss2.ResumableDownloadStore(store),
                                      num_threads=threading.active_count())
        t = thread_upload()
        t.start()
        t.join()

    def put_file(self):
        pass

    def get_file_list(self):
        object_list = oss2.ObjectIterator(self.bucket)
        return object_list




if __name__ == '__main__':
    oss = OSS(AccessKeyID="LTAI4G9dLAHHnSuQzwje6vpH", AccessKeySecret="6nXrhDV2JCeG5eszPKFdNZJOOCkbNt",
              EndPoint="http://oss-cn-beijing.aliyuncs.com",
              BucketName="hzdata-entfiles")


    # oss.get_file()
    # file_list = oss.get_file_list()
    # for i in file_list:
    #     print(i.key, i.size)

    # start = time.time()
    file_name = "GR20203724290"
    oss.get_file_by_resumable(object_name="{}.pdf".format(file_name), local_path="C:\\Users\\Administrator\\Desktop\\Project\\autoflow\\original_files\\{}.pdf".format(file_name))
    # # oss.get_file_by_resumable(object_name="GR2020509580.pdf", local_path="C:\\Users\\Administrator\\Desktop\\Project\\autoflow\\original_files\\GR2020509580.pdf")
    # # oss.put_file_by_resumable(object_name="test.pdf", local_path="C:\\Users\\Administrator\\Desktop\\test.pdf")
    # # oss.get_file_by_resumable(object_name="test.pdf", local_path="C:\\Users\\Administrator\\Desktop\\Project\\autoflow\\original_files\\test.pdf")
    # oss.get_file_by_stream(object_name="test.pdf", local_path="C:\\Users\\Administrator\\Desktop\\Project\\autoflow\\original_files\\test.pdf")
    # end = time.time()
    # print("总共耗时: {}".format(end - start))
